package cn.com.controller;

import cn.com.pojo.UserBean;
import cn.com.service.UserService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
    @RequestMapping("/All")
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping(value = "/users",method = RequestMethod.POST)
    @ResponseBody
    public Object findAll(){
        List<UserBean> list = userService.findAll();
        return JSONArray.toJSONString(list);
    }
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    @ResponseBody
    public Object getByTel(@RequestParam("telephone") String telephone){
        UserBean userBean = userService.getByTel(telephone);
        return JSONArray.toJSONString(userBean);
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(UserBean userBean){
        String msg = "";
        if (userService.add(userBean)>0){
            msg = "成功";
        }else {
            msg = "失败";
        }
        return JSONArray.toJSONString(msg);
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(UserBean userBean){
        String msg = "";
        if (userService.update(userBean)>0){
            msg = "成功";
        }else {
            msg = "失败";
        }
        return JSONArray.toJSONString(msg);
    }
}
